<!doctype html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title>数据统计</title>
    <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/public.css') }}" />
    <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/fupan_css.css') }}" />
    <script src="{{ url_for('static',filename='js/update.js') }}"></script>
    <script src="{{ url_for('static',filename='js/jquery-2.1.1.min.js') }}"></script>
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
</head>

<body style="height: 100%; margin: 0">
    <div class="table" style="float:left;margin-right:10px;" align="center">
        <table id="table" cellspacing="0"></table>
    </div>
    <!-- <div id="line_2" style="float:left;margin-right:10px;" align="center"></div> -->
    <div id="line_1" style="float:left;height:300px;width:600px"></div>
    <div id="line_2" style="float:left;height:300px;width:600px"></div>
    <!-- <div id="line_3" style="float:left;height:300px;width:600px"></div>
    <div id="line_4" style="float:left;height:300px;width:600px"></div> -->
    <div id="line_5" style="float:left;height:300px;width:600px"></div>
    <div id="line_6" style="float:left;height:300px;width:600px"></div>
    <div id="statisticsMinutes" style="float:left;height:300px;width:600px"></div>
    <div id="bar" style="float:left;height:300px;width:600px"></div>
    <script src="{{ url_for('static',filename='js/echarts.min.js') }}"></script>
    <script src="{{ url_for('static',filename='js/jquery-2.1.1.min.js') }}"></script>
</body>

<script type="text/javascript">
    $(document).ready(function() {
        statisticsMinutes_line()
        auto_update(statisticsMinutes_line)
        line_market_volume()
        auto_update(line_market_volume) //市场量能
        m_date = flask('history_date', '23')['date'] //回溯统计天数，从最近的一个交易日前推
        line(m_date, "line_1", '非一字首板', '平均涨幅', '股票总和') //问财非一字首板统计
        line(m_date, "line_2", '非一字板连板股', '平均涨幅', '股票总和') //问财非一字板连板股统计
            // line(m_date, "line_3", '非一字首板', search_1 = '开盘涨幅', rose = '分时涨跌幅') //问财非一字板连板股统计
            // line(m_date, "line_4", '非一字板连板股', search_1 = '开盘涨幅', rose = '分时涨跌幅') //问财非一字板连板股统计
        search(m_date, "line_5", '涨停连续涨停天数排序 ', rose = '连续涨停天数') //rose 查询的匹配头
        date = '10'
        data = flask('cls', date) //财联社大盘异动统计
        console.log(data)
        bar(data, 'bar', date + '日大盘异动统计')
    });
</script>
<script>
    function statisticsMinutes_line() {
        data = []
        data.push(_statisticsMinutes())
        data.push(_statisticsLastMinutes(_history_date()[1]))
        _statisticsMinutes_line(data, 'statisticsMinutes', '涨跌停1分钟数据统计', '涨停', '昨日涨停', '跌停', '昨日跌停')
    }
    //特定日期的涨跌停1分钟数据统计
    function _statisticsLastMinutes(_data) {
        var data = []
        $.ajax({
            url: '/statisticsLastMinutes',
            data: {
                data: _data
            },
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(_data) {
                data = _data['data']
                eval(data) //执行字符串语句，获取变量
                data = min['Data']
                data = _statisticsMinutes_decode_data(data)
            },
            error: function(msg) {
                console.log(msg)
            }
        })
        return data
    }

    //涨跌停1分钟数据统计
    function _statisticsMinutes() {
        var data = []
        $.ajax({
            url: '/statisticsMinutes',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(_data) {
                data = _data['data']
                eval(data) //执行字符串语句，获取变量
                data = min['Data']
                data = _statisticsMinutes_decode_data(data)
            },
            error: function(msg) {
                console.log(msg)
            }
        })
        return data
    }

    function _statisticsMinutes_decode_data(data) {
        var times = [] //时间
        var limits = [] //涨停
        var downs = [] //跌停

        for (var i = 0; i < data.length; i++) {
            times.push(data[i][0])
            limits.push(data[i][1])
            downs.push(data[i][2])
        }
        return {
            'date': times,
            'count_limit': limits,
            'count_down': downs
        }
    }

    function _statisticsMinutes_line(data, div, title, legend_1 = '1', legend_2 = '2', legend_3 = '3', legend_4 = '4') {
        var dom = document.getElementById(div);
        var myChart = echarts.init(dom);
        var app = {};

        var option;

        option = {
            title: {
                left: 'center',
                text: title
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                right: 'right',
                data: [legend_1, legend_2, legend_3, legend_4],
                orient: 'vertical'
            },
            xAxis: {
                type: 'category',
                data: data[0].date,
                axisLabel: {
                    interval: function(index, value) {
                        if (value === '925') {
                            return '9:25'
                        } else if (value === '1000') {
                            return '10:00'
                        } else if (value === '1030') {
                            return '10:30'
                        } else if (value === '1100') {
                            return '11:00'
                        } else if (value === '1130') {
                            return '11:30'
                        } else if (value === '1330') {
                            return '13:30'
                        } else if (value === '1400') {
                            return '14:00'
                        } else if (value === '1430') {
                            return '14:30'
                        } else if (value === '1500') {
                            return '15:00'
                        }
                    },
                    formatter: function(value, index) {
                        return value.slice(0, -2).concat(':', value.slice(-2))
                    }
                }
            },
            yAxis: {
                type: 'value',
            },
            series: [{
                name: legend_1,
                data: data[0].count_limit,
                type: 'line',
                smooth: true,
                label: {
                    show: true,
                    position: 'top'
                },
                color: 'rgb(255,0,0)',
                symbol: 'none',
                endLabel: {
                    show: true,
                    formatter: '{c}'
                }
            }, {
                name: legend_2,
                data: data[1].count_limit,
                type: 'line',
                smooth: true,
                label: {
                    show: true,
                    position: 'top'
                },
                color: 'rgb(137,137,137)',
                symbol: 'none',
                endLabel: {
                    show: true,
                    formatter: '{c}'
                }
            }, {
                name: legend_3,
                data: data[0].count_down,
                type: 'line',
                // yAxisIndex: 1,
                smooth: true,
                label: {
                    show: true,
                    position: 'top'
                },
                color: 'rgb(0,128,64)',
                symbol: 'none',
                endLabel: {
                    show: true,
                    formatter: '{c}'
                }
            }, {
                name: legend_4,
                data: data[1].count_down,
                type: 'line',
                // yAxisIndex: 1,
                smooth: true,
                label: {
                    show: true,
                    position: 'top'
                },
                color: 'rgb(137,137,137)',
                symbol: 'none',
                endLabel: {
                    show: true,
                    formatter: '{c}'
                }
            }]
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }
</script>
<script>
    function line_market_volume() {
        date = _history_date()
        data = _market_volume()
        line_market_volume_echarts(data, 'line_6', '市场量能增加：' + data['subtract'] + '亿', label_show = false, legend_1 = date[0], legend_2 = date[1])
    }
    // 市场量能
    function _market_volume() {
        var data = []
        $.ajax({
            url: '/market_volume',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(_data) {
                data = _market_volume_decode_data(_data)
            },
            error: function(msg) {
                console.log(msg)
            }
        })
        return data
    };

    // 市场量能
    function _market_volume_decode_data(_data) {
        _data = _data['data']['info']['trends']

        m_m_date = []
        m_m_data = []
        m_m_data_rose = []
        m_m_data_total = []
        for (var i = 0; i < _data.length; i++) {
            m_m_date.push(_data[i][0])
            m_m_data_rose.push((_data[i][1] / 10000).toFixed(2))
            m_m_data_total.push((_data[i][2] / 10000).toFixed(2))
                // m_m_data.push(m_data)
        }
        m_m_data = {
            rose: m_m_data_rose,
            total: m_m_data_total
        }
        data = {
            'date': m_m_date,
            'data': m_m_data,
            'subtract': ((_data[_data.length - 1][1] / 10000) - (_data[_data.length - 1][2] / 10000)).toFixed(2)
        }
        return data
    };
</script>
<script type="text/javascript">
    // 查询
    function search(m_date, div, search, rose = '涨跌幅') {
        m_echarts = []
        m_m_date = []
        m_m_data = []
        m_m_data_rose = []
        m_m_data_total = []
        for (var i = m_date.length - 1; i >= 0; i--) {
            data = flask('statistics', m_date[i] + search + ' 去掉ST 上市天数大于30; ')
            m_data = _search(data['data'], rose + '[' + m_date[i].replaceAll('-', '') + ']')
            m_m_date.push(m_date[i])
            m_m_data_rose.push(m_data.search)
        }
        m_m_data = {
            rose: m_m_data_rose,
        }
        m_echarts = {
            'date': m_m_date,
            'data': m_m_data
        }
        line_echarts(m_echarts, div, search)
    }

    //_search
    function _search(data, search = '涨跌幅') {
        stock_search = data['columns'].indexOf(search)

        return {
            search: data['data'][0][stock_search]
        }
    }

    // 画线
    function line(m_date, div, search, legend_1, legend_2, search_1 = '涨跌幅', rose = '涨跌幅') {
        m_echarts = []
        m_m_date = []
        m_m_data = []
        m_m_data_rose = []
        m_m_data_total = []
        for (var i = m_date.length - 1; i >= 0; i--) {
            data = flask('statistics', m_date[i] + search + ' 去掉ST 上市天数大于30   ' + m_date[i - 1] + search_1)
            if (i == 0) {
                m_data = set_table(data['data'], m_date[i], rose)
            } else
                m_data = set_table(data['data'], m_date[i], rose + ':前复权[' + m_date[i - 1].replaceAll('-', '') + ']')
            m_m_date.push(m_date[i])
            m_m_data_rose.push(m_data.rose)
            m_m_data_total.push(m_data.total)
                // m_m_data.push(m_data)
        }
        m_m_data = {
            rose: m_m_data_rose,
            total: m_m_data_total
        }
        m_echarts = {
            'date': m_m_date,
            'data': m_m_data
        }
        line_echarts(m_echarts, div, search + search_1, true, legend_1, legend_2)
    }

    // 柱状图
    function bar(data, bar, title) {
        dataX = data['counts']['index'].slice(0, 6)
        dataY = data['counts']['data'].slice(0, 6)
        bar_echarts({
            dataX: dataX,
            dataY: dataY
        }, bar, title)
    }

    // ajax 发送post请求
    function flask(m_function, search) {
        var m_data = []
        $.ajax({
            url: '/' + m_function,
            data: {
                data: search
            },
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    function set_table(data, date, rose = '涨跌幅') {
        all_theme = []
        var m_html = ""
        var m_th = "<tr class=\"cur\">\
            <th>股票简称</th>\
            <th>股票代码</th>\
            <th>涨跌幅</th>\
            <th>连续涨停天数</th>\
            </tr>"
        var m_list = data['data']
        stock_name = data['columns'].indexOf('股票简称')
        stock_code = data['columns'].indexOf('股票代码')
        stock_rose = data['columns'].indexOf(rose)
        stock_day = data['columns'].indexOf('连续涨停天数')
        var m_average = 0

        for (var i_row = 0; i_row < m_list.length; i_row++) {
            if (i_row % 2 == 0) {
                m_html += "<tr>"
            } else
                m_html += "<tr class=\"cur\">"

            m_html += "<td>" + m_list[i_row][stock_name] + "</td>"
            m_html += "<td>" + m_list[i_row][stock_code] + "</td>"
                // 设置颜色
            if (m_list[i_row][stock_rose] > 0) {
                m_html += "<td class=\"red\">" + m_list[i_row][stock_rose] + "</td>"
            } else
                m_html += "<td class=\"gre\">" + m_list[i_row][stock_rose] + "</td>"

            if (m_list[i_row][stock_rose] == null) {
                m_average += parseFloat(0)
            } else
                m_average += parseFloat(m_list[i_row][stock_rose])

            m_html += "<td>" + m_list[i_row][stock_day] + "</td>"

            m_html += "</tr>"
        }

        var tb = document.getElementById("table"); //获取表格的dom节点
        // var caption = "<caption align=\"top\">连板(" + m_list.length + ")" + m_average / m_list.length + "</caption>"
        var caption = '<tr><td>' + date + '</td>' + '<td>' + '连板' + m_list.length + '</td>' + '<td>' + m_average / m_list.length + '</td>' + '</tr>'
            // tb.innerHTML += caption + m_th + m_html

        return {
            rose: (m_average / m_list.length).toFixed(2),
            total: m_list.length
        }
    }
</script>

<script type="text/javascript">
    // 获取连板接力股数据
    function line_2_sort() {
        $.ajax({
            url: '/line_2_sort',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                line_echarts(data['data'])
            },
            error: function(msg) {
                console.log(msg)
            }
        })
    };

    //处理数据
    function decode_data(data) {
        console.log(data)
        var m_array = []
        var m_data = []
        var m_name = []

        for (var i = 0; i < data.length; i++) {
            m_array.push(data[i][0])
            m_name.push(data[i][1])
            m_data.push(data[i][3])
        }
        return {
            dimensions: m_array,
            source: m_data,
            name: m_name
        }
    }

    function line_echarts(data, div, title, label_show = true, legend_1 = '1', legend_2 = '2') {
        var dom = document.getElementById(div);
        var myChart = echarts.init(dom);
        var app = {};

        var option;

        option = {
            title: {
                left: 'center',
                text: title
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                left: 'left',
                data: [legend_1, legend_2]
            },
            xAxis: {
                type: 'category',
                data: data.date
            },
            yAxis: [{
                type: 'value'
            }, {
                type: 'value'
            }, ],
            series: [{
                name: legend_1,
                data: data.data.rose,
                type: 'line',
                smooth: true,
                label: {
                    show: label_show,
                    position: 'top'
                },
            }, {
                name: legend_2,
                data: data.data.total,
                type: 'line',
                yAxisIndex: 1,
                smooth: true,
                label: {
                    show: label_show,
                    position: 'top'
                },
            }]
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

    function line_market_volume_echarts(data, div, title, label_show = true, legend_1 = '1', legend_2 = '2') {
        var dom = document.getElementById(div);
        var myChart = echarts.init(dom);
        var app = {};

        var option;

        option = {
            title: {
                left: 'center',
                text: title
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                right: 'right',
                data: [legend_1, legend_2],
                orient: 'vertical'
            },
            xAxis: {
                type: 'category',
                data: data.date,
                axisLabel: {
                    interval: 29,
                }
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    formatter: '{value}亿'
                },
            },
            series: [{
                name: legend_1,
                data: data.data.rose,
                type: 'line',
                smooth: true,
                label: {
                    show: label_show,
                    position: 'top'
                },
                color: 'rgb(255,0,0)',
                symbol: 'none',
                endLabel: {
                    show: true,
                    formatter: '{c}'
                }
            }, {
                name: legend_2,
                data: data.data.total,
                type: 'line',
                // yAxisIndex: 1,
                smooth: true,
                label: {
                    show: label_show,
                    position: 'top'
                },
                color: 'rgb(130,130,130)',
                symbol: 'none',
                endLabel: {
                    show: true,
                    formatter: '{c}'
                }
            }]
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

    function bar_echarts(data, bar, title) {
        var dom = document.getElementById(bar);
        var myChart = echarts.init(dom);
        var app = {};

        var option;

        option = {
            title: {
                left: 'center',
                text: title
            },
            xAxis: {
                type: 'category',
                data: data.dataX
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: data.dataY,
                type: 'bar'
            }]
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }

    }

    // if (option && typeof option === 'object') {
    //     myChart.setOption(option);
    // }
</script>

</html>